/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solve = function(board) {
  let canChange = new Array(board.length);
  for(let i = 0; i < board.length; i++) {
    canChange[i] = new Array(board[0].length).fill(true);
  }

  function dfs(indexI, indexJ) {
    canChange[indexI][indexJ] = false;
    // 上
    if(indexI - 1 >= 0 && board[indexI - 1][indexJ] == 'O' && canChange[indexI - 1][indexJ]) {
      dfs(indexI - 1, indexJ);
    }
    // 下
    if(indexI + 1 < board.length && board[indexI + 1][indexJ] == 'O' && canChange[indexI + 1][indexJ]) {
      dfs(indexI + 1, indexJ);
    }
    // 左
    if(indexJ - 1 >= 0 && board[indexI][indexJ - 1] == 'O' && canChange[indexI][indexJ - 1]) {
      dfs(indexI, indexJ - 1);
    }
    // 右
    if(indexJ + 1 < board[0].length && board[indexI][indexJ + 1] == 'O' && canChange[indexI][indexJ + 1]) {
      dfs(indexI, indexJ + 1);
    }
  }
  // 第一行与最后一行
  let i = 0;
  for(let j = 0; j < board[0].length; j++) {
    if(board[i][j] == 'O' && canChange[i][j])
      dfs(i, j);
  }
  i = board.length - 1;
  for(let j = 0; j < board[0].length; j++) {
    if(board[i][j] == 'O' && canChange[i][j])
      dfs(i, j);
  }
  let j = 0;
  for(let i = 1; i < board.length; i++) {
    if(board[i][j] == 'O' && canChange[i][j])
      dfs(i, j);
  }
  j = board[0].length - 1;
  for(let i = 1; i < board.length; i++) {
    if(board[i][j] == 'O' && canChange[i][j])
      dfs(i, j);
  }
  for(let i = 1; i < board.length - 1; i++) {
    for(let j = 1; j < board[0].length - 1; j++) {
      if(canChange[i][j] && board[i][j] == 'O')
        board[i][j] = 'X';
    }
  }
  return board;
};
let board = [
  ["X","X","X","X"],
  ["X","O","O","X"],
  ["X","X","O","X"],
  ["X","O","X","X"]
];
console.log(solve(board));
board = [["X"]];
console.log(solve(board));
board =
[["O","O"],["O","O"]];
console.log(solve(board));
board = [["O","O","O"],["O","O","O"],["O","O","O"]];
console.log(solve(board));

